<?php
if (!defined("_INDEX")) { header("Location: /"); die; }

if (!($city_id = $route->getAdminId()))
	goToPage("/admin/city");

$errors = array();

if ($route->POST('action') == 'edit')
{
	$name = $route->POST('name');
	$name_rod = $route->POST('name_rod');
	$name_pred = $route->POST('name_pred');
	$path = $route->POST('path');
	$phone_code = $route->POST('phone_code');
	$country = $route->POST('country');
	
	$paths = $db->getCell("select `path` from `city` where `path` = '$path' and `city_id` != '$city_id' limit 1");
	if ($paths != null)
		$errors[] = 'Уже существует город с таким путем!';
	
	if (count($errors) == 0)
	{
		$query = "update `city` set `name` = '$name',`path`='$path',`name_rod`='$name_rod',`name_pred`='$name_pred',`phone_code`='$phone_code',`country`='$country' where `city_id` = '$city_id' limit 1";
		$db->sendQuery($query);
	}
}

$city = $db->getRow("select * from `city` where `city_id` = '$city_id' limit 1");
if ($city == null)
	goToPage("/admin/city");

loadPage("head.php");
?>

<h1>Редактирование города</h1><br /><?php
	if (count($errors) > 0)
		echo "<p class='fail'>".implode("<br />",$errors)."<br /><br /></p>";
	elseif ($route->POST('action') == 'edit')
		echo "<p class='success'>Успешно отредактировано<br /><br /></p>";
?>
<form method='POST'>
	<input type='hidden' name='action' value='edit' />
	<table cellpadding='3' cellspacing='5'>
		<tr>
			<td width='170px'>Название</td>
			<td width='300px'><input type='text' name='name' value='<?php echo $city['name']; ?>' /></td>
		</tr>
		<tr>
			<td>Название в род. падеже</td>
			<td><input type='text' name='name_rod' value='<?php echo $city['name_rod']; ?>' /></td>
		</tr>
		<tr>
			<td>Название в пред. падеже</td>
			<td><input type='text' name='name_pred' value='<?php echo $city['name_pred']; ?>' /></td>
		</tr>
		<tr>
			<td>Путь</td>
			<td><input type='text' name='path' value='<?php echo $city['path']; ?>' /></td>
		</tr>
		<tr>
			<td>Телефонный код</td>
			<td><input type='text' name='phone_code' value='<?php echo $city['phone_code']; ?>' /></td>
		</tr>
		<tr>
			<td>Страна</td>
			<td>
				<select name='country'>
					<option value='1'>Россия</option>
				</select>
			</td>
		</tr>
		<tr>
			<td colspan='2' align='right'><input type='submit' value='Редактировать' /></td>
		</tr>
	</table>
</form>

<?php loadPage("footer.php"); ?>